package com.zeronx.i18n;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.MessageSource;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.i18n.CookieLocaleResolver;
import org.springframework.web.servlet.i18n.SessionLocaleResolver;

import javax.servlet.http.HttpServletRequest;
import java.util.Locale;

/**
 * @author foreee on 2017/3/30.
 * @version 1.0
 */
@Component
public class I18nMsg {

    /**
     * Name of the session attribute that holds the Locale.
     * Only used internally by this implementation.
     * <p>Use {@code RequestContext(Utils).getLocale()}
     * to retrieve the current locale in controllers or views.
     * @see org.springframework.web.servlet.support.RequestContext#getLocale
     * @see org.springframework.web.servlet.support.RequestContextUtils#getLocale
     */
    public static final String LOCALE_SESSION_ATTRIBUTE_NAME = SessionLocaleResolver.class.getName() + ".LOCALE";

    /**
     * Name of the session attribute that holds the TimeZone.
     * Only used internally by this implementation.
     * <p>Use {@code RequestContext(Utils).getTimeZone()}
     * to retrieve the current time zone in controllers or views.
     * @see org.springframework.web.servlet.support.RequestContext#getTimeZone
     * @see org.springframework.web.servlet.support.RequestContextUtils#getTimeZone
     */
//    public static final String TIME_ZONE_SESSION_ATTRIBUTE_NAME = SessionLocaleResolver.class.getName() + ".TIME_ZONE";

    /**
     * The name of the request attribute that holds the Locale.
     * <p>Only used for overriding a cookie value if the locale has been
     * changed in the course of the current request!
     * <p>Use {@code RequestContext(Utils).getLocale()}
     * to retrieve the current locale in controllers or views.
     * @see org.springframework.web.servlet.support.RequestContext#getLocale
     * @see org.springframework.web.servlet.support.RequestContextUtils#getLocale
     */
    public static final String LOCALE_REQUEST_ATTRIBUTE_NAME = CookieLocaleResolver.class.getName() + ".LOCALE";

    /**
     * The name of the request attribute that holds the TimeZone.
     * <p>Only used for overriding a cookie value if the locale has been
     * changed in the course of the current request!
     * <p>Use {@code RequestContext(Utils).getTimeZone()}
     * to retrieve the current time zone in controllers or views.
     * @see org.springframework.web.servlet.support.RequestContext#getTimeZone
     * @see org.springframework.web.servlet.support.RequestContextUtils#getTimeZone
     */
//    public static final String TIME_ZONE_REQUEST_ATTRIBUTE_NAME = CookieLocaleResolver.class.getName() + ".TIME_ZONE";

    /**
     * The default cookie name used if none is explicitly set.
     */
//    public static final String DEFAULT_COOKIE_NAME = CookieLocaleResolver.class.getName() + ".LOCALE";


    @Autowired
    private HttpServletRequest request;
    @Autowired
    private MessageSource messageSource;

    /**
     * 根据属性名称获取i18n配置文件的值
     *
     * @param properties
     * @return
     */
    public String getValBy(String properties) {
        Locale locale = getLocale(request);
        return messageSource.getMessage(properties, null, "", locale);
    }
    private Locale getLocale(HttpServletRequest request) {
        Locale locale2 = (Locale) request.getAttribute(LOCALE_REQUEST_ATTRIBUTE_NAME);
        if (null == locale2) {
            locale2 = (Locale) request.getAttribute(LOCALE_SESSION_ATTRIBUTE_NAME);
        }
        if (null == locale2) {
            return request.getLocale();
        }
        return locale2;
    }
}